package com.jackrain.nea.ext.sequence.util;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.ad.api.AdsequenceQueryCmd;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.ext.sequence.observer.SequenceExec;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.web.face.User;
import com.jackrain.nea.web.face.impl.UserImpl;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySession;
import com.jackrain.nea.web.query.QueryUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Locale;

/**
 * @author xubin
 * 2017-11-15
 */
@Component
@Slf4j
public class SequenceGenUtil {
    @Reference(version = "1.0",group = "ad")
    private AdsequenceQueryCmd AdsequenceQueryCmd;


    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, String tableName, String columnName, Long id, Object mapperBean,String methodName) {
        return preGenerateSequence().add(sequenceName,object, id, mapperBean,methodName);
    }

    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, Long id, Object mapperBean,String methodName, Locale locale) {
        return preGenerateSequence().add(sequenceName,object, id, mapperBean,methodName,locale);
    }

    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, Long id, Object mapperBean,String methodName, boolean isTest) {
        return preGenerateSequence().add(sequenceName,object, id, mapperBean,methodName,isTest);
    }

    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, Long id, Object mapperBean,String methodName, Locale locale, boolean isTest) {
        return preGenerateSequence().add(sequenceName,object, id, mapperBean,methodName,locale,isTest);
    }

    //
    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, String tableName, String columnName, Long id,Class[] extendTypes,Object[] extendParams, Object mapperBean,String methodName) {
        return preGenerateSequence().add(sequenceName,object, id,extendTypes,extendParams, mapperBean,methodName);
    }

    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, Long id,Class[] extendTypes,Object[] extendParams, Object mapperBean,String methodName, Locale locale) {
        return preGenerateSequence().add(sequenceName,object, id,extendTypes,extendParams, mapperBean,methodName,locale);
    }

    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, Long id,Class[] extendTypes,Object[] extendParams, Object mapperBean,String methodName, boolean isTest) {
        return preGenerateSequence().add(sequenceName,object, id,extendTypes,extendParams, mapperBean,methodName,isTest);
    }

    public static SequenceExec preGenerateSequence(String sequenceName, JSONObject object, Long id,Class[] extendTypes,Object[] extendParams, Object mapperBean,String methodName, Locale locale, boolean isTest) {
        return preGenerateSequence().add(sequenceName,object, id,extendTypes,extendParams, mapperBean,methodName,locale,isTest);
    }

    public static SequenceExec preGenerateSequence(){
        SequenceExec subject = new SequenceExec();
        return subject;
    }

    private static void validateParam(String sequenceName, String sql, Long id) {
        assert StringUtils.isNotBlank(sequenceName) : Resources.getMessage("序号生成器名称不能为空");
        Assert.notNull(id, Resources.getMessage("ID不能为空"));
        assert id > 0 : Resources.getMessage("ID不能小于等于0");
        assert StringUtils.isNotBlank(sql) : Resources.getMessage("sql语句不能为空");
    }


    public static String generateSquence(String sequenceName, JSONObject object, Locale locale, boolean isTest) {




        User user = new UserImpl();

        QuerySession querySession = QueryUtils.createQuerySession(user,null,locale);

        JSONObject map= new JSONObject();
        map.put("sequenceName",sequenceName);
        map.put("isTest",isTest);
        map.put("fixcolumn",object);
        DefaultWebEvent event = new DefaultWebEvent("querySequence",map);
//        event.put("param", map);
        querySession.setEvent(event);

        Object reference = ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext(), "com.jackrain.nea.ad.api.AdsequenceQueryCmd", "ad", "1.0");
        return ((AdsequenceQueryCmd)reference).execute(querySession).get("result").toString();
    }




}
